#{include file="../Common/Header.html"}
<style>
    .layui-layer-page .layui-layer-content {
        position: relative !important;
        overflow: auto !important;
    }
</style>
<div class="layui-container fly-marginTop fly-user-main">
    #{include file="../Common/Nav.html"}
    <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <div class="content-header">
                <i class="layui-icon">&#xe638;</i> 我的商品
            </div>

            <div class="content-body">
                <div style="margin-bottom: 5px;margin-top: 15px;">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-pink button-add"><i class="layui-icon"
                                                                                                      style="position: relative;top: -2px;">&#xe624;</i>添加商品
                    </button>
                </div>
                <form class="search-query"></form>
                <table id="commodity" lay-filter="commodity"></table>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        layui.use(['hex'], function () {
            let table = $("#commodity");
            let cao = layui.hex;
            var _category;
            let category = function () {
                if (!_category) {
                    _category = User.$postSync("/user/api/category/data");
                }
                let dict = [];
                _category.forEach(item => {
                    dict.push({id: item.id, name: item.name});
                })
                return dict;
            }

            let queryParams = null;
            let commodityUniqueId = 0;
            table.bootstrapTable({
                url: '/user/api/commodity/data',//请求的url地址
                method: "post",//请求方式
                pageSize: 7,//每页显示的数量
                showRefresh: false,//是否显示刷新按钮
                cache: false,//是否使用缓存
                showToggle: false,//是否显示详细视图和列表视图的切换按钮
                cardView: false,
                pagination: true,//是否显示分页
                pageNumber: 1,//初始化显示第几页，默认第1页
                singleSelect: false,//复选框只能选择一条记录
                sidePagination: 'server',//分页显示方式，可以选择客户端和服务端（server|client）
                contentType: "application/x-www-form-urlencoded",//使用post请求时必须加上
                dataType: "json",//接收的数据类型
                queryParamsType: 'limit',//参数格式，发送标准的Restful类型的请求
                queryParams: function (params) {
                    params.page = (params.offset / params.limit) + 1;
                    if (queryParams) {
                        for (const key in params) {
                            queryParams[key] = params[key];
                        }
                    } else {
                        queryParams = params;
                    }
                    return queryParams;
                },
                //回调函数
                responseHandler: function (res) {
                    return {
                        "total": res.count,
                        "rows": res.data
                    }
                },
                columns: [
                    {field: 'id', title: '商品ID'}
                    , {
                        field: 'category', title: '分类', formatter: function (val, item) {
                            if (!item.category) {
                                return '-';
                            }
                            return '<span style="color: #0C84D1;">' + item.category.name + '</span>'
                        }
                    }
                    , {field: 'name', title: '商品名称'}
                    , {
                        field: 'card_count', title: '库存', formatter: function (val, item) {
                            if (item.delivery_way == 0 && !item.shared_id) {
                                return item.card_count;
                            }
                            return '-';
                        }
                    }
                    , {
                        field: 'card_success_count', title: '已售', formatter: function (val, item) {
                            if (item.delivery_way == 0 && !item.shared_id) {
                                return item.card_success_count;
                            }
                            return '-';
                        }
                    }
                    , {
                        field: 'price', title: '零售价', formatter: function (val, item) {
                            return '<b style="color: green;">' + item.price + '</b>';
                        }
                    }
                    , {
                        field: 'user_price', title: '会员价', formatter: function (val, item) {
                            return '<span style="color: mediumvioletred;">' + item.user_price + '</span>';
                        }
                    }
                    , {
                        field: 'status', title: '状态', formatter: function (val, item) {
                            if (item.status == 1) {
                                return '<span class="acg-badge acg-badge-light-success">已上架</span>';
                            }
                            return '<span class="acg-badge acg-badge-light">已下架</span>';
                        }
                    }
                    , {
                        field: 'delivery_way', title: '发货方式', formatter: function (val, item) {
                            if (item.delivery_way == 1) {
                                return '<span class="acg-badge acg-badge-light">手动发货</span>';
                            }
                            return '<span class="acg-badge acg-badge-light-success">自动发货</span>';
                        }
                    }
                    , {
                        field: 'share_url', title: '推广链接', formatter: function (val, item) {
                            return '<span  class="layui-badge layui-bg-gray clipboard clipboard-' + item.id + '" data-clipboard-text="' + item.share_url + '" style="cursor: pointer;">复制</span>';
                        }, events: {
                            'click .clipboard': function (event, value, row, index) {
                                let clipboard = new ClipboardJS(".clipboard-" + row.id);
                                clipboard.on('success', function (e) {
                                    layer.msg("分享链接已经复制成功了，赶快发给好友吧！");
                                });
                            }
                        }
                    }
                    , {
                        field: 'operate',
                        title: '',
                        formatter: function (val, item) {
                            let html = '<span  class="acg-badge acg-badge-light-primary acg-badge-button edit"><i class="layui-icon">&#xe642;</i> 修改</span> ';
                            html += '<span class="acg-badge acg-badge-light-red acg-badge-button del"><i class="layui-icon">&#xe640;</i> 删除</span>';
                            return html;
                        },
                        events: {
                            'click .edit': function (event, value, row, index) {
                                modal(row);
                            },
                            'click .del': function (event, value, row, index) {
                                layer.confirm('您正在移除该商品，是否要继续？', {
                                    btn: ['确认移除', '取消']
                                }, function () {
                                    $.post('/user/api/commodity/del', {id: row.id}, res => {
                                        layer.msg(res.msg);
                                        table.bootstrapTable('refresh', {silent: true});
                                    });
                                });
                            }
                        }
                    }
                ]
            });

            let modal = (values = {}) => {
                cao.popup('/user/api/commodity/save', [
                    {
                        title: "商品图标",
                        name: "cover",
                        type: "image",
                        placeholder: "请选择图片(推荐64*64)",
                        uploadUrl: User.property.uploadUrl
                    },
                    {
                        title: "商品名称", name: "name", type: "input", placeholder: "商品名称",
                        required: true
                    },
                    {
                        title: "商品说明",
                        name: "description",
                        type: "editor",
                        placeholder: "商品说明",
                        uploadUrl: User.property.uploadUrl,
                        required: true
                    },
                    {
                        title: "商品分类",
                        name: "category_id",
                        type: "select",
                        dict: category(),
                        placeholder: "请选择分类",
                        required: true
                    },
                    {title: "成本价", name: "factory_price", type: "input", default: 0.00},
                    {title: "商品单价", name: "price", type: "input", placeholder: "商品单价(未登录游客购买价)/0=免费",
                        required: true},
                    {title: "会员价", name: "user_price", type: "input", placeholder: "会员价(登录注册后购买的价格)/0=免费",
                        required: true},
                    {
                        title: "发货方式",
                        name: "delivery_way",
                        type: "select",
                        placeholder: "请选择",
                        dict: [{id: 0, name: "自动发货"}, {id: 1, name: "手动发货/插件"}],
                        default: 0,
                        required: true,
                        change: function (value) {
                            let d1 = cao.popupElement("delivery_message", "textarea", commodityUniqueId).parent().parent();
                            let d2 = cao.popupElement("delivery_auto_mode", "select", commodityUniqueId).parent().parent();
                            if (value == 1) {
                                d1.show();
                                d2.hide();
                            } else {
                                d1.hide();
                                d2.show();
                            }
                        }
                    },
                    {
                        title: "卡密排序",
                        name: "delivery_auto_mode",
                        type: "select",
                        placeholder: "请选择",
                        dict: [{id: 0, name: "优先旧卡(有序)"}, {id: 1, name: "随机发卡(无序)"}, {id: 2, name: "优先新卡(有序)"}],
                        default: 0,
                        hide: true
                    },
                    {
                        title: "发货信息",
                        name: "delivery_message",
                        type: "textarea",
                        placeholder: "手动发货信息，可以是一些固定的卡密或者软件下载链接等..",
                        hide: true
                    },
                    {
                        title: "发货留言",
                        name: "leave_message",
                        type: "textarea",
                        placeholder: "当用户购买商品后，该留言会显示在订单中",
                        tips: "当用户购买商品后，该留言会显示在订单中"
                    },
                    {
                        title: "联系方式",
                        name: "contact_type",
                        type: "select",
                        placeholder: "请选择",
                        dict: [{id: 0, name: "任意"}, {id: 1, name: "手机号"}, {id: 2, name: "邮箱"}, {id: 3, name: "QQ号"}],
                        default: 0,
                        required: true
                    },
                    {
                        title: "邮件发送",
                        name: "send_email",
                        type: "switch",
                        text: "启用",
                        tips: "用户购买商品后，会将卡密信息发送至邮箱，仅联系方式为邮箱状态下有效。"
                    },
                    {
                        title: "查询密码",
                        name: "password_status",
                        type: "switch",
                        text: "启用",
                        tips: "开启后，下单时需要设置查询订单的密码，更强的保护用户隐私"
                    },
                    {title: "排序", name: "sort", type: "input", placeholder: "排序(最低1000起)，越小越靠前", default: 1000},
                    {title: "优惠卷", name: "coupon", type: "switch", text: "启用"},
                    {
                        title: "限时秒杀", name: "seckill_status", type: "switch", text: "启用", change: function (value) {
                            let d1 = cao.popupElement("seckill_start_time", "date", commodityUniqueId).parent().parent();
                            let d2 = cao.popupElement("seckill_end_time", "date", commodityUniqueId).parent().parent();
                            if (value == 1) {
                                d1.show();
                                d2.show();
                            } else {
                                d1.hide();
                                d2.hide();
                            }
                        }
                    },
                    {title: "秒杀开始", name: "seckill_start_time", type: "date", placeholder: "开始时间", hide: true},
                    {title: "秒杀结束", name: "seckill_end_time", type: "date", placeholder: "结束时间", hide: true},
                    {
                        title: "卡密预选", name: "draft_status", type: "switch", text: "启用", change: function (value) {
                            let d1 = cao.popupElement("draft_premium", "input", commodityUniqueId).parent().parent();
                            if (value == 1) {
                                d1.show();
                            } else {
                                d1.hide();
                            }
                        }, tips: "顾名思义，意思就是顾客在购买时，可以预先选择想要购买的那个卡密，一般针对于出售游戏账号等用途。"
                    },
                    {
                        title: "预选时加价",
                        name: "draft_premium",
                        type: "input",
                        placeholder: "请输入预选时加价的金额",
                        hide: true
                    },
                    {
                        title: "强制登录",
                        name: "only_user",
                        type: "switch",
                        text: "启用",
                        tips: "用户必须登录后才能购买商品"
                    },
                    {
                        title: "限购数量",
                        name: "purchase_count",
                        type: "input",
                        placeholder: "0代表不限制",
                        tips: "0代表不限制，如果限制了购买数量，那么用户必须登录才能购买。",
                        default: 0,
                    },
                    {
                        title: "隐藏库存",
                        name: "inventory_hidden",
                        type: "switch",
                        text: "启用",
                        tips: "该功能开启后，自动发货的商品将不会再显示具体库存"
                    },
                    {
                        title: "控件",
                        name: "widget",
                        type: "custom",
                        tips: "该功能可以在店铺前台自定义输入框<br><p>1.中文名：推荐4个字</p><p>2.键名：外部插件可以通过这个名称获取到值</p><p>3.提示内容：控件提示的内容</p><p>4.正则：通过正则表达式来限制用户提交内容，正则表达式可以网上搜索如何使用，不限制可留空</p><p>5.匹配错误提示：正则匹配失败，将会这里填写的错误信息</p>"
                    },
                    {title: "配置参数", name: "config", type: "textarea", placeholder: "配置参数", height: 260},
                    {title: "状态", name: "status", type: "switch", text: "上架"},
                    #{hook(\App\Consts\Hook::USER_VIEW_COMMODITY_POST)}
                ], res => {
                    table.bootstrapTable('refresh', {silent: true});
                }, values, ["720px", "720px"], false, "添加", uniqueId => {
                    commodityUniqueId = uniqueId;
                    //success
                    let d = cao.popupElement("draft_premium", "input", uniqueId).parent().parent();
                    cao.popupElement("draft_status", "input", uniqueId).val() == 1 ? d.show() : d.hide();

                    let d1 = cao.popupElement("delivery_auto_mode", "select", uniqueId).parent().parent();
                    cao.popupElement("delivery_way", "select", uniqueId).val() == 0 ? d1.show() : d1.hide();
                });
            }

            $('.button-add').click(function () {
                modal();
            });

            cao.query('.search-query', table, [
                {title: "商品分类", name: "equal-category_id", type: "select", dict: category(), search: true},
                {title: "商品名称(模糊搜索)", name: "search-name", type: "input"},
                {
                    title: "上架状态", name: "equal-status", type: "select", dict: [
                        {id: 0, name: "已下架"},
                        {id: 1, name: "已上架"}
                    ]
                }
            ], true, false);

        });
    });
</script>
#{include file="../Common/Footer.html"}
